package com.shopping.www.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.shopping.www.model.Brand;
import com.shopping.www.model.BrandAndNum;
import com.shopping.www.service.AdminService;
import com.shopping.www.service.BrandService;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {
    private BrandService service1=new BrandService();
    //查询商城的在售商品
    public void selectAllBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("content-type","application:json;charset=utf8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods","POST");
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
        BufferedReader br=request.getReader();
        String params=br.readLine();
        Brand brand=JSON.parseObject(params,Brand.class);
        //调用BrandService完成查询
        List<Brand> brands=service1.selectAllBrand(brand);
        //存入request域中
        //1.request.setAttribute("brands",brands);
        //将集合转为JSON数据  (对象序列化为数据)
        String jsonString=JSON.toJSONString(brands, SerializerFeature.IgnoreErrorGetter);
        //将字符串响应到页面上
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //查询某个商品的数量
    public void selectBusinessNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        id=new String(id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _id=Integer.parseInt(id);
        int shelveNum=service1.selectBusinessNum(_id);
        String jsonString=JSON.toJSONString(shelveNum);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //加入购物车
    public void addToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int id1=Integer.parseInt(brand_id);
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int id2=Integer.parseInt(user_id);
        String cartNum=request.getParameter("cartNum");
        cartNum=new String(cartNum.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _cartNum=Integer.parseInt(cartNum);
        service1.addToCart(id1,id2,_cartNum);
    }
    //根据分类查询商城的所有商品
    public void selectAllSortBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("content-type","application:json;charset=utf8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods","POST");
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
        String belong=request.getParameter("belong");
        belong=new String(belong.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        BufferedReader br=request.getReader();
        String params=br.readLine();
        Brand brand=JSON.parseObject(params,Brand.class);
        List<Brand> brands=service1.selectAllSortBrand(brand,belong);
        String jsonString=JSON.toJSONString(brands);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //查询购物车商品
    public void selectCartBrands(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        BrandAndNum<Brand> brandAndNum=service1.selectCartBrands(_user_id);
        String jsonString=JSON.toJSONString(brandAndNum,SerializerFeature.DisableCircularReferenceDetect);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //查询购物车最大商品数量，即不能添加超过货架在售数量
    public void selectCartBrandMaxNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        int[] maxNum=service1.selectCartBrandMaxNum(_user_id);
        String jsonString=JSON.toJSONString(maxNum);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //查询要加入购物车的商品状态，如果已在购物车请不要再次购买
    public void checkCartBrandStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int id1=Integer.parseInt(brand_id);
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int id2=Integer.parseInt(user_id);
        int status =service1.checkCartBrandStatus(id1,id2);
        if(status==1){
            response.getWriter().write("fail");
        }
    }
    //在购物车修改商品数量
    public void updateCartNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int id1=Integer.parseInt(brand_id);
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int id2=Integer.parseInt(user_id);
        String num=request.getParameter("num");
        num=new String(num.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _num=Integer.parseInt(num);
        service1.updateCartNum(id1,id2,_num);
    }
    //删除购物车商品
    public void deleteCartByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader br=request.getReader();
        String params=br.readLine();
        //2.将JSON字符串转为int数组
        int[] ids=JSON.parseObject(params,int[].class);
//        System.out.println(Arrays.asList(strIds).stream().mapToInt(Integer::parseInt).toArray());
//        int[] intIds=new int[strIds.length];//创建一个int数组
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        service1.deleteCartByIds(ids,_user_id);
    }
    //直接购买
     public void addToOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int id1=Integer.parseInt(brand_id);
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int id2=Integer.parseInt(user_id);
        String orderNum=request.getParameter("orderNum");
        orderNum=new String(orderNum.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _orderNum=Integer.parseInt(orderNum);
        String flag=request.getParameter("flag");
        flag=new String(flag.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _flag=Integer.parseInt(flag);
        int b=service1.addToOrder(id1,id2,_orderNum,_flag);
        if(b==1){
            response.getWriter().write("success");
        }
        if(b==2){
            response.getWriter().write("extend");
        }
        if(b==3){
            response.getWriter().write("fail");
        }
    }
    //选择配送服务
    public void chooseDelivery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _brand_id=Integer.parseInt(brand_id);
        String maxNum=request.getParameter("maxNum");
        maxNum=new String(maxNum.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _maxNum=Integer.parseInt(maxNum);
        String num=request.getParameter("num");
        num=new String(num.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _num=Integer.parseInt(num);
//        System.out.println(_brand_id);
        service1.chooseDelivery(_user_id,_brand_id,_maxNum,_num);
    }
    //查询订单商品
    public void selectOrderBrands(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        BrandAndNum<Brand> brandAndNum=service1.selectOrderBrands(_user_id);
        String jsonString=JSON.toJSONString(brandAndNum,SerializerFeature.DisableCircularReferenceDetect);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //删除购买记录
    public void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        String flag=request.getParameter("flag");
        flag=new String(flag.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _flag=Integer.parseInt(flag);
        service1.deleteOrder(_user_id,_flag);
    }
    //查询选择配送的商品
    public void selectUserDelivery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        BrandAndNum<Brand> brandAndNum=service1.selectUserDelivery(_user_id);
        String jsonString=JSON.toJSONString(brandAndNum,SerializerFeature.DisableCircularReferenceDetect);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //收货
    public void finishDelivery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _brand_id=Integer.parseInt(brand_id);
        service1.finishDelivery(_user_id,_brand_id);
    }
    //修改密码
    public void updatePass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id=request.getParameter("user_id");
        user_id=new String(user_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _user_id=Integer.parseInt(user_id);
        String password=request.getParameter("password");
        password=new String(password.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        service1.updatePass(_user_id,password);
    }
}
